package cn.wolfcode.wms.web.controller;


import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.query.QueryForMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService systemMenuService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryForMenu parentId,Model model){

        //parentId的值是否为null,如果为null,那么表示就是在根菜单,如果不为null,那么表示在子菜单中
        if (parentId.getParentId()!= null) {

            List<SystemMenu> mens = systemMenuService.getParentMenu(parentId.getParentId());
            model.addAttribute("mens",mens);
        }

        model.addAttribute("list",systemMenuService.query(parentId));

        return "systemmenu/list";
    }

    @RequestMapping("input")
    public String input(Long id,Model model,Long parentId){

        if (parentId != null) {
            model.addAttribute("parent",systemMenuService.get(parentId));
        }
        if(id != null){
            model.addAttribute("entity",systemMenuService.get(id));
        }

        return "systemmenu/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){

        systemMenuService.delete(id);

        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(SystemMenu d){

        systemMenuService.saveOrUpdate(d);

        return new JSONResult();
    }

    @RequestMapping("getMenuSn")
    @ResponseBody
    public Object getMenuSn(String menuSn){
        //因为集合转成json会返回数组
        List<Map<String,Object>> list = systemMenuService.getSystemMenu(menuSn);

        return list;
    }
}
